package com.buddy.sds.auth.mapper.result;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buddy.sds.auth.entity.SystemDict;
import com.buddy.sds.auth.entity.SystemDictItem;
import com.buddy.sds.auth.mapper.ISystemDictItemMapper;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;

import java.util.ArrayList;
import java.util.List;

public class SystemDictCascasdeResultHandler implements ResultHandler<SystemDict> {

    private List<SystemDict> systemDicts = new ArrayList<>();

    public List<SystemDict> getSystemDicts() {
        return systemDicts;
    }

    private ISystemDictItemMapper dictItemMapper;

    public SystemDictCascasdeResultHandler(ISystemDictItemMapper systemDictItemMapper) {

        this.dictItemMapper = systemDictItemMapper;
    }

    @Override
    public void handleResult(ResultContext<? extends SystemDict> resultContext) {

        SystemDict dict = resultContext.getResultObject();

        List<SystemDictItem> items = dictItemMapper.selectList(new QueryWrapper<SystemDictItem>().eq("item_dict", dict.getDictId()));

        if (items != null && !items.isEmpty()) {
            dict.setDictItems(items);
        }

        systemDicts.add(dict);
    }
}
